package fonkin.com.wifisignapp.adapter.news;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.Collection;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import fonkin.com.wifisignapp.R;
import fonkin.com.wifisignapp.bean.news.NewsInfor;
import fonkin.com.wifisignapp.bean.user.User;
import fonkin.com.wifisignapp.constants.AppConstants;

import static com.squareup.picasso.MemoryPolicy.NO_CACHE;
import static com.squareup.picasso.MemoryPolicy.NO_STORE;

/**
 * @Description:新闻列表Adapter
 * @Author: huanghongjing
 * @Date: 2018/4/7 23:43
 * No such property: code for class: Script1
 *
 */

public class UserAdapter extends BaseAdapter {
    private Context context;
    private List<User> userInfoList;

    public UserAdapter(Context context, List<User> userInfoList) {
        this.context = context;
        this.userInfoList = userInfoList;
    }

    @Override
    public int getCount() {
        return userInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return userInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.user_item,parent,false);
            convertView.setTag(new Viewholder(convertView));
        }
        Viewholder tag = (Viewholder) convertView.getTag();

        User user = userInfoList.get(position);
        tag.user_name.setText(user.getName());


        Picasso
                .with(context)
                .load(AppConstants.IMAGE_API+user.getHeadImg())
                .placeholder(R.drawable.loading_pic)
                .resize(80,50)
                .error(R.drawable.no_pic)
                .memoryPolicy(NO_CACHE, NO_STORE)
                .config(Bitmap.Config.RGB_565)
                .onlyScaleDown()
                .into(tag.imag);
        return convertView;
    }

    public void addAll(Collection<? extends User> collection){
        userInfoList.addAll(collection);
        notifyDataSetChanged();
    }
    public static  class Viewholder{
        private final CircleImageView imag;
        private final TextView user_name;

        public  Viewholder(View item){
            imag = ((CircleImageView) item.findViewById(R.id.dept_user_pic));
            user_name = ((TextView) item.findViewById(R.id.dept_user_name));

        }
    }
}
